home *** CD-ROM | disk | FTP | other *** search
- Path: news.acadia.net!usenet
- From: steven2@salesbook.com (Steve Nutt)
- Newsgroups: comp.lang.c++
- Subject: Re: How to copy an object?
- Date: Sun, 03 Mar 1996 06:21:24 GMT
- Organization: DET
- Message-ID: <4hb52h$8jd@post.acadia.net>
- References: <4h05rb$su6@atlas.tncnet.com>
- Reply-To: steven2@salesbook.com
- NNTP-Posting-Host: blf7.acadia.net
- X-Newsreader: Forte Free Agent 1.0.82
-
- Simon Lee (Simon Lee) wrote:
-
-
- >Hello,
-
- >I'm trying to have a member function return a duplicate copy of a
- >member data object. How would I go about this?
-
- >Ie.
-
- >class x {
- >private:
- > Y *object_of_class_y;
-
- >public:
- > Y GetY();
- >};
-
-
- >I would like GetY() to return a copy of object_of_class_y, not a pointer
- >to it.
-
- >Any email responses appreciated.
-
- >-Simon
-
-
- You will need a copy constructor for class Y. You can then do either
-
- class x {
- pribate:
- Y* object_of_class_y;
-
- public:
- Y GetY ()
- {
- CHECK (object_of_class_y); // check that
- object_of_class_y != 0
- return *object_of_class_y;
- }
- };
-
- or you could make GetY return a const Y&. This would be faster.
-
- Steve
-
-